package com.cskaoyan.mall.dao.mall;

import com.cskaoyan.mall.model.bo.admin.UpdateUserDataBO;
import com.cskaoyan.mall.model.bo.mall.SignUpBO;
import com.cskaoyan.mall.model.bo.mall.UpdatePwdBO;
import com.cskaoyan.mall.model.bo.mall.UserLoginBO;
import com.cskaoyan.mall.model.vo.mall.GetTokenVO;
import com.cskaoyan.mall.model.vo.mall.UserDataVO;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class MallUserDaoImpl implements MallUserDao {
    @Override
    /*
address: "北京市"
email: "1511213@qq.com"
nickname: "Wangfanfanfan"
phone: "13578888888"
pwd: "Wangfan!1"
recipient: "智障"
 */
    public int signup(SignUpBO signUpBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into user (email,nickname,pwd,recipient,address,phone) values (?,?,?,?,?,?)",
                    signUpBO.getEmail(),
                    signUpBO.getNickname(),
                    signUpBO.getPwd(),
                    signUpBO.getRecipient(),
                    signUpBO.getAddress(),
                    signUpBO.getPhone());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }

    @Override
    public int login(UserLoginBO loginBO) {
        //JDBC
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Long query = null;
        try {
            query = (Long) runner.query("select count(id) from user where email = ? and pwd = ?",
                    new ScalarHandler(),loginBO.getEmail(),loginBO.getPwd());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 500;
        }
        return query != 0 ? 200 : 404;
    }
    /*
        private Integer id;
    private String email;
    private String nickname;
    private String recipient;
    private String address;
    private String phone;
    private Integer code = 0;

    传来的token是email
     */
    @Override
    public UserDataVO data(String token) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        UserDataVO data = null;
        try {
            data = runner.query("select id , email , nickname , recipient , address , phone from user where email = ? ",new BeanHandler<>(UserDataVO.class),token);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return data;
    }

    @Override
    public int updatePwd(UpdatePwdBO pwd) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update user set pwd = ? where id = ? ",
                    pwd.getConfirmPwd(),
                    pwd.getAdminToken());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }
    /*
{id: 3,
nickname: "Sandy",
recipient: "曾",
address: "北京",
phone: "13567899876"}
 */
    @Override
    public int updateUserData(UpdateUserDataBO data) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update user set nickname = ?, recipient = ? , address = ? , phone  = ? where id = ? ",
                    data.getNickname(),
                    data.getRecipient(),
                    data.getAddress(),
                    data.getPhone(),
                    data.getId()
                    );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 404;
        }
        return 200;
    }

    @Override
    public int logoutClient(GetTokenVO data) {
//        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
//        try {
//            runner.update("update user set nickname = ?, recipient = ? , address = ? , phone  = ? where id = ? ",
//                    data.getNickname(),
//                    data.getRecipient(),
//                    data.getAddress(),
//                    data.getPhone(),
//                    data.getId()
//            );
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//            return 404;
//        }
        return 200;
    }
}
